package 寻找数组的中心下标;

class Solution {
    //方法一
    public int pivotIndex1(int[] nums) {
        int ret = -1;
        int len = nums.length;
        int[] dp = new int[len];
        for(int i = 0; i < len; i++){
            if(i == 0){
                dp[i] = nums[i];
            }else{
                dp[i] = dp[i - 1] + nums[i];
            }
        }
        for(int i = 0; i < len; i++){
            if(i == 0 && (dp[len - 1] - dp[0]) == 0){
                ret = 0;
                break;
            }else if(i != 0 && dp[i - 1] == (dp[len - 1] - dp[i])){
                ret = i;
                break;
            }
        }
        return ret;
    }

    //方法二 ->精简
    public int pivotIndex(int[] nums) {
        int len = nums.length;
        int[] f = new int[len];
        int[] g = new int[len];
        for(int i = 1; i < len; i++){
            f[i] = f[i - 1] + nums[i - 1];
            g[len - 1 - i] = g[len - i] + nums[len - i];
        }
        for(int i = 0; i < len; i++){
            if(f[i] == g[i]){
                return i;
            }
        }
        return -1;
    }
}